#!/usr/bin/env ruby
#
# This file is gererated by ruby-glade-create-template 1.1.4.
#
## Copyright (C) 2007 Janek Thomaschewski <janek@php4you.de>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

begin
  require 'rubygems'
rescue LoadError
  puts "couldn't find rubygems library, continuing without it"
end

begin
  require 'webrick'
rescue LoadError
  puts "couldn't find webrick library, continuing without it and will not have operating system check"
end

require 'libglade2'

class OperatingSystem
  class << self
    def host
      Config::CONFIG['host']
    end

    def macos?
      host.include?('darwin')
    end

    def linux?
      host.include?('linux')
    end

    def windows?
      host.include?('mswin')
    end

    def current
      case
        when macos?:   'macos'
        when linux?:   'linux'
        when windows?: 'windows'
        else           'unknown'
      end
    end
  end
end

class Ubuntu_lampp_control
  include GetText

  attr :glade

  def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
    #bindtextdomain(domain, localedir, nil, "UTF-8")
    bindtextdomain(domain, nil, nil, "UTF-8")
    @glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler| method(handler)}

    @glade.get_widget('window1').show
    @entry_result = @glade.get_widget('entry_result')
    @textview_output_buffer = @glade.get_widget('textview_output').buffer
    @textview_output = @glade.get_widget('textview_output')
	check_run(false, false)
	check_autorun
  end

  ''' Methods '''

  def check_autorun
	Thread.new('check_autorun') {
		f = IO.popen('ls /etc/rc2.d')
	    res = f.readlines.join('')

	    if res.match('S20apache2')
		    @glade.get_widget('image_apache_autorun_status').stock = 'gtk-yes'
		else
			@glade.get_widget('image_apache_autorun_status').stock = 'gtk-no'
		end

		if res.match('S20mysql')
		    @glade.get_widget('image_mysql_autorun_status').stock = 'gtk-yes'
		else
			@glade.get_widget('image_mysql_autorun_status').stock = 'gtk-no'
		end

		Process.kill("SIGALRM", f.pid)
		Process.kill("TERM", f.pid)
	}
  end

  def check_run(action = false,thread = true)
    if thread
	    Thread.new('check_run') {
	    	t = 5
	    	t.times {
			    f = IO.popen('ps aux')
			    res = f.readlines.join('')

			    if res.match('/usr/sbin/apache2')
				    @glade.get_widget('image_apache_status').stock = 'gtk-yes'
				else
					@glade.get_widget('image_apache_status').stock = 'gtk-no'
				end

				if res.match('/usr/sbin/mysqld')
				    @glade.get_widget('image_mysql_status').stock = 'gtk-yes'
				else
					@glade.get_widget('image_mysql_status').stock = 'gtk-no'
				end

				Process.kill("SIGALRM", f.pid)
				Process.kill("TERM", f.pid)
				sleep(1)
			}
		}
	else
	    f = IO.popen('ps aux')
	    res = f.readlines.join('')

	    if res.match('/usr/sbin/apache2')
		    @glade.get_widget('image_apache_status').stock = 'gtk-yes'
		else
			@glade.get_widget('image_apache_status').stock = 'gtk-no'
		end

		if res.match('/usr/sbin/mysqld')
		    @glade.get_widget('image_mysql_status').stock = 'gtk-yes'
		else
			@glade.get_widget('image_mysql_status').stock = 'gtk-no'
		end

		Process.kill("SIGALRM", f.pid)
	end
  end

  def control_init(name, action, title = '')
    if title.length == 0
      title = name
    end

    if action == 'start'
      success = _('%s has been started...') % title
      failour = _('%s failed to start...') % title
    elsif action == 'stop'
      success = _('%s has been stopped...') % title
      failour = _('%s failed to stop...') % title
    else
      success = 'unknown'
      failour = 'unknown'
    end

    f = IO.popen('gksu \'/etc/init.d/' + name + ' ' + action + '\'')
    res = f.readlines.join("\n")
    if res.match('[ OK ]')
      @entry_result.text = success
    else
      @entry_result.text = failour
    end
    @textview_output_buffer.text = res
    @textview_output.set_buffer(@textview_output_buffer)
    check_run(name)
  end

  def control_autorun(name, action, title = '')
    if title.length == 0
      title = name
    end

    if action == 'defaults'
      success = _('Autorun for %s has been enabled...') % title
      failour = _('Autorun for %s could not be enabled...') % title
      already_enabled = _('Autorun for %s is already enabled...') % title
      already_disabled = ''
    elsif action == 'remove'
      success = _('Autorun for %s has been disabled...') % title
      failour = _('Autorun for %s could not be disabled...') % title
      already_enabled = ''
      already_disabled = _('Autorun for %s is already disabled...') % title
    else
      success = 'unknown'
      failour = 'unknown'
      already_enabled = ''
      already_disabled = ''
    end

    f = IO.popen('gksu \'update-rc.d -f ' + name + ' ' + action + '\'')
    res = f.readlines.join("\n")
    if res.match('/etc/rc2.d/')
      @entry_result.text = success
    elsif res.match('already exist')
      @entry_result.text = already_enabled
    elsif action == 'remove' && res.length != 0
      @entry_result.text = already_disabled
    else
      @entry_result.text = failour
    end
    @textview_output_buffer.text = res
    @textview_output.set_buffer(@textview_output_buffer)
    check_autorun
  end

  ''' Events '''
  def on_window1_destroy(widget)
    puts _('Window has been closed')
    Gtk.main_quit
  end

  def on_button_mysql_start_clicked(widget)
    control_init('mysql', 'start', _('MySQL'))
  end
  def on_button_mysql_stop_clicked(widget)
    control_init('mysql', 'stop', _('MySQL'))
  end


  def on_button_apache_start_clicked(widget)
    control_init('apache2', 'start', _('Apache 2'))
  end
  def on_button_apache_stop_clicked(widget)
    control_init('apache2', 'stop', _('Apache 2'))
  end

  def on_button_mysql_enable_clicked(widget)
    control_autorun('mysql', 'defaults', _('MySQL'))
  end
  def on_button_mysql_disable_clicked(widget)
    control_autorun('mysql', 'remove', _('MySQL'))
  end

  def on_button_apache_enable_clicked(widget)
    control_autorun('apache2', 'defaults', _('Apache 2'))
  end
  def on_button_apache_disable_clicked(widget)
    control_autorun('apache2', 'remove', _('Apache 2'))
  end

end

# Main program
if __FILE__ == $0
  if OperatingSystem.current != 'linux'
	puts 'Only Linux is supported by this app!'
  	exit
  end
  # Set values as your own application.
  PROG_PATH = "../data/ubuntu_lampp_control.glade"
  PROG_NAME = "ubuntu_lampp_control"
  Ubuntu_lampp_control.new(PROG_PATH, nil, PROG_NAME, "/usr/share/locale")
  Gtk.main
end
